package com.example.qxfw.common.dmgis.layer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmgis.common.*;
import com.dmgis.work.AreaWork;
import com.dmgis.work.TextWork;
import com.dmgis.work.VecWorkShop;
import com.example.qxfw.common.dmgis.constant.ContourMapType;
import com.example.qxfw.common.dmgis.edit.AreaEdit;
import com.example.qxfw.common.dmgis.edit.TextEdit;
import com.example.qxfw.common.dmgis.entity.ContourSetting;
import com.example.qxfw.common.dmgis.entity.EqProjectConfig;
import com.example.qxfw.common.dmgis.vo.DtVo;
import com.example.qxfw.common.dmgis.vo.TextVo;
import com.example.qxfw.common.util.NumberFormat;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class ESWindContourMapLayer extends ContourMapLayer{
    @Override
    public void titleLayer() {
        EqProjectConfig projectConfig = projectConfigMapper.selectOne(new QueryWrapper<>());
        if (projectConfig!=null){
            //标题起始点
            Dt dt1 = new Dt(81.46134, 44.74545);
            Dt dt2 = new Dt(81.36269, 44.57870);

            VecWorkShop vecWorkShop = new VecWorkShop();
            short ai = vecWorkShop.CreateTextWork(projectConfig.getTextPath());
            TextWork textWork=(TextWork)vecWorkShop.GetWorkByNo(ai);

            TInf titleInf = new TInf();
            titleInf.Setszfont((byte) 1);
            titleInf.Sethei(0.16f);
            titleInf.Setwid(0.08f);
            titleInf.Setcol(0);
            titleInf.Setbackclr(0);

            TInf timeInf = new TInf();
            timeInf.Setszfont((byte) 12);
            timeInf.Sethei(0.12f);
            timeInf.Setwid(0.06f);
            timeInf.Setcol(0);
            timeInf.Setbackclr(0);

            textWork.AddObj(ContourMapType.E_S_WIND.getTitle(), dt1,titleInf);

            Calendar instance = Calendar.getInstance();
            instance.setTime(new Date());
            instance.add(Calendar.DAY_OF_MONTH,-1);
            Date time = instance.getTime();
            String format="yyyy年MM月dd日HH时";
            textWork.AddObj(DateFormatUtils.format(time,format)+" 至 "+DateFormatUtils.format(new Date(),format), dt2,timeInf);

            textWork.SaveWork();

            dt1.Delete();
            dt2.Delete();
            titleInf.Delete();
            timeInf.Delete();
            vecWorkShop.delete();
        }
    }

    @Override
    public void legendLayer() {
        List<ContourSetting> contourSettings = contourSettingMapper.selectList(new LambdaQueryWrapper<ContourSetting>().eq(ContourSetting::getElementtype, ContourMapType.E_S_WIND.getName()).orderByDesc(ContourSetting::getElementvalue));

        if (contourSettings!=null&&!contourSettings.isEmpty()) {
            EqProjectConfig eqProjectConfig = projectConfigMapper.selectOne(new QueryWrapper<>());
            if (eqProjectConfig != null) {
                //设置临时路径
                DmMapCom dmMapCom = new DmMapCom();
                dmMapCom.setTempPath(tempPath);

                VecWorkShop vecWorkShop = new VecWorkShop();
                short ai = vecWorkShop.CreateAreaWork(eqProjectConfig.getLegendPath());
                AreaWork areaWork = (AreaWork) vecWorkShop.GetWorkByNo(ai);

                //起始点
                DtVo dtVo = new DtVo(84.33426, 42.94039);

                //创建一个线实体
                LInf linf = new LInf((short) -1, (byte) 0, 10, 10, 0, (byte) 1);
                //创建一个面实体
                AInf aInf = new AInf((short) 1, contourSettings.get(0).getElementcolor(), contourSettings.get(0).getElementcolor(), linf);
                AreaEdit.drawRectangle(areaWork, dtVo, 0.23063, -0.11284, aInf);
                for (int i = 1; i < contourSettings.size(); i++) {
                    long rgb = contourSettings.get(i).getElementcolor();
                    aInf.SetPatClr(rgb);
                    aInf.SetCol(rgb);
                    AreaEdit.copyObj(areaWork, i - 1, 0, -0.128389, aInf);
                }
                linf.Delete();
                aInf.Delete();
                vecWorkShop.delete();
            }
        }
    }

    @Override
    public void legendTextLayer() {
        //单位起始点
        DtVo unitDtVo = new DtVo(84.413778, 42.940019);

        float yOffset=0.130389f;

        //x坐标
        double y=42.199827;
        double x=84.30248;
        double fontXSize=-0.030342;

        TInf info=new TInf();
        info.Setbackclr(0);
        info.Setcol(0);
        info.Setwid(0.03f);
        info.Sethei(0.06f);
        info.Setheavy((byte) 1);

        List<ContourSetting> contourSettings = contourSettingMapper.selectList(new LambdaQueryWrapper<ContourSetting>().eq(ContourSetting::getElementtype, ContourMapType.E_S_WIND.getName()).orderByAsc(ContourSetting::getElementvalue));
        if (contourSettings!=null&&!contourSettings.isEmpty()){
            EqProjectConfig eqProjectConfig = projectConfigMapper.selectOne(new QueryWrapper<>());

            if (eqProjectConfig!=null){
                ArrayList<TextVo> list = new ArrayList<>();

                for (int i=0;i<contourSettings.size();i++){
                    String content;
                    ContourSetting contourSetting = contourSettings.get(i);
                    if ((i+1)==contourSettings.size()){
                        content=">"+ NumberFormat.formatNumber(contourSetting.getElementvalue());
                    }else {
                        content= NumberFormat.formatNumber(contourSetting.getElementvalue())+"-"+NumberFormat.formatNumber(contourSettings.get(i+1).getElementvalue());
                    }
                    if (i!=0){
                        y+=yOffset;
                    }
                    double x1=x+(content.length()*fontXSize);
                    TextVo textVo = new TextVo(x1, y, content);
                    list.add(textVo);
                }

                list.add(new TextVo(unitDtVo.getX(),unitDtVo.getY(), ContourMapType.E_S_WIND.getUnit()));
                TextEdit.batchAddObj(eqProjectConfig.getLegendTextPath(),list,info);
            }

        }
        info.Delete();
    }
}
